package com.wuxiaolong.backend.config;

//import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.wuxiaolong.backend.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
//import org.springframework.format.FormatterRegistry;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
//import org.springframework.validation.MessageCodesResolver;
//import org.springframework.validation.Validator;
//import org.springframework.web.method.support.HandlerMethodArgumentResolver;
//import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
//import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Description:
 *
 * @author Leo
 * @date 2020-04-19
 */

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

    private TokenInterceptor tokenInterceptor;

    //构造方法
    public WebConfiguration(TokenInterceptor tokenInterceptor){
        this.tokenInterceptor = tokenInterceptor;
    }

    /**
     * 跨域问题解决   springboot 1.5 解决办法
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "OPTIONS", "PUT","DELETE","PATCH")
                .allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin","Authorization", "Access-Control-Request-Method", "Access-Control-Request-Headers")
                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
                .allowCredentials(true).maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //排除拦截，除了注册登录(此时还没token)，其他都拦截  //登录 注册 静态资源
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/register","/login");
        super.addInterceptors(registry);
    }
}
